//
//  AdvancedCell.m
//  Courses
//
//  Created by xxx on 15/1/9.
//  Copyright (c) 2015年 wtf. All rights reserved.
//

#import "AdvancedCell.h"
#import "UIImageView+WebCache.h"
#import "CourseClass.h"
#import "NSDate+WTF.h"

@implementation AdvancedCell
{
    UIImageView *_imgViewAvatar;
    UILabel *_lblTeacherName;
    UILabel *_lblCourseName;
    
    UILabel *_lblLessonHours;
    UILabel *_lblClassPrice;
    UILabel *_lblClass1;
    UILabel *_lblClass2;
    UILabel *_lblClass3;
    UIButton *_btnCls1;
    UIButton *_btnCls2;
    UIButton *_btnCls3;
}

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (!self) return self;
    self.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    
    self.backgroundColor = nil;
    self.frame = AutoRectMake(0, 0, DEFAULT_WIDTH, 80, WTFAutoLayoutTypePic);
    
    _selectedClsIndex = 1;
    
    _imgViewAvatar = [[UIImageView alloc] initWithFrame:AutoRectMake(10, 10, 80, 60, WTFAutoLayoutTypeRaw)];
    _imgViewAvatar.layer.cornerRadius = 17;
    _imgViewAvatar.layer.masksToBounds = YES;
    _imgViewAvatar.contentMode = UIViewContentModeScaleAspectFit;
    [self.contentView addSubview:_imgViewAvatar];
    
    _lblTeacherName = [[UILabel alloc] initWithFrame:AutoRectMake(100, 10, 100, 20, WTFAutoLayoutTypeBar)];
    _lblTeacherName.font = [UIFont fontWithName:nil size:FONT_SIZE_TITLE];
    [self.contentView addSubview:_lblTeacherName];
    
    _lblCourseName = [[UILabel alloc] initWithFrame:AutoRectMake(160, 10, DEFAULT_WIDTH - 180, 20, WTFAutoLayoutTypeBar)];
    _lblCourseName.font = [UIFont fontWithName:nil size:FONT_SIZE_TITLE];
    [self.contentView addSubview:_lblCourseName];
    
    _lblLessonHours = [[UILabel alloc] initWithFrame:AutoRectMake(100, 30, 100, 12, WTFAutoLayoutTypeBar)];
    _lblLessonHours.font = [UIFont fontWithName:nil size:FONT_SIZE_CONTENT];
    [self.contentView addSubview:_lblLessonHours];
    
    _lblClassPrice = [[UILabel alloc] initWithFrame:AutoRectMake(100, 42, 100, 12, WTFAutoLayoutTypeBar)];
    _lblClassPrice.textColor = [UIColor orangeColor];
    _lblClassPrice.font = [UIFont fontWithName:nil size:FONT_SIZE_CONTENT];
    [self.contentView addSubview:_lblClassPrice];
    
    UILabel *lblClassesPrefix = [[UILabel alloc] initWithFrame:AutoRectMake(100, 54, 50, 16, WTFAutoLayoutTypeBar)];
    lblClassesPrefix.text = @"开课 : ";
    lblClassesPrefix.font = [UIFont fontWithName:nil size:FONT_SIZE_SUBTITLE];
    [self.contentView addSubview:lblClassesPrefix];
    
    CGRect rectCls1 = AutoRectMake(150, 54, 30, 16, WTFAutoLayoutTypeBar);
    _lblClass1 = [[UILabel alloc] initWithFrame:rectCls1];
    _lblClass1.textAlignment = NSTextAlignmentCenter;
    _lblClass1.font = [UIFont fontWithName:nil size:FONT_SIZE_SUBTITLE];
    [self.contentView addSubview:_lblClass1];
    _lblClass1.hidden = YES;
    _btnCls1 = [[UIButton alloc] initWithFrame:rectCls1];
    _btnCls1.layer.borderColor = [WTFColors defaultIconColor].CGColor;
    _btnCls1.layer.borderWidth = 1;
    _btnCls1.hidden = YES;
    [_btnCls1 addTarget:self action:@selector(selectCls1) forControlEvents:UIControlEventTouchUpInside];
    [self.contentView addSubview:_btnCls1];
    
    CGRect rectCls2 = AutoRectMake(200, 54, 30, 16, WTFAutoLayoutTypeBar);
    _lblClass2 = [[UILabel alloc] initWithFrame:rectCls2];
    _lblClass2.textAlignment = NSTextAlignmentCenter;
    _lblClass2.font = [UIFont fontWithName:nil size:FONT_SIZE_SUBTITLE];
    [self.contentView addSubview:_lblClass2];
    _lblClass2.hidden = YES;
    _btnCls2 = [[UIButton alloc] initWithFrame:rectCls2];
    _btnCls2.layer.borderColor = [UIColor blackColor].CGColor;
    _btnCls2.layer.borderWidth = 1;
    _btnCls2.hidden = YES;
    [_btnCls2 addTarget:self action:@selector(selectCls2) forControlEvents:UIControlEventTouchUpInside];
    [self.contentView addSubview:_btnCls2];
    
    CGRect rectCls3 = AutoRectMake(250, 54, 30, 16, WTFAutoLayoutTypeBar);
    _lblClass3 = [[UILabel alloc] initWithFrame:rectCls3];
    _lblClass3.textAlignment = NSTextAlignmentCenter;
    _lblClass3.font = [UIFont fontWithName:nil size:FONT_SIZE_SUBTITLE];
    [self.contentView addSubview:_lblClass3];
    _lblClass3.hidden = YES;
    _btnCls3 = [[UIButton alloc] initWithFrame:rectCls3];
    _btnCls3.layer.borderColor = [UIColor blackColor].CGColor;
    _btnCls3.layer.borderWidth = 1;
    _btnCls3.hidden = YES;
    [_btnCls3 addTarget:self action:@selector(selectCls3) forControlEvents:UIControlEventTouchUpInside];
    [self.contentView addSubview:_btnCls3];
    
    return self;
}

- (void)setCourse:(AdvancedCourse *)course
{
    _course = course;
    
    _lblClass1.hidden = YES;
    _btnCls1.hidden = YES;
    _lblClass2.hidden = YES;
    _btnCls2.hidden = YES;
    _lblClass3.hidden = YES;
    _btnCls3.hidden = YES;
    
    _lblCourseName.text = course.courseName;
    _lblTeacherName.text = course.teacherName;
    [_imgViewAvatar sd_setImageWithURL:[NSURL URLWithString:course.teacherHeadPhotoUrl]];
    _lblLessonHours.text = [NSString stringWithFormat:@"课时 : %@", course.lessonHours];
    if (course.courseClasses.count)
    {
        _lblClassPrice.text = [NSString stringWithFormat:@"价格 : ￥%@", [course.courseClasses[0] classPrice]];
        for (int i = 1; i <= course.courseClasses.count; i++)
        {
            NSString *classOpenDate = [NSDate monthStringForDateString:[course.courseClasses[i - 1] classOpenDate]];
            switch (i) {
                case 1:
                    _lblClass1.text = classOpenDate;
                    _lblClass1.hidden = NO;
                    _btnCls1.hidden = NO;
                    break;
                case 2:
                    _lblClass2.text = classOpenDate;
                    _lblClass2.hidden = NO;
                    _btnCls2.hidden = NO;
                    break;
                case 3:
                    _lblClass3.text = classOpenDate;
                    _lblClass3.hidden = NO;
                    _btnCls3.hidden = NO;
                    break;
                default:
                    break;
            }
        }
    }
}

- (void)selectCls1
{
    NSLog(@"cls 1");
    [self selectClsAtIndex:1];
}

- (void)selectCls2
{
    NSLog(@"cls 2");
    [self selectClsAtIndex:2];
}

- (void)selectCls3
{
    NSLog(@"cls 3");
    [self selectClsAtIndex:3];
}

- (void)selectClsAtIndex:(NSInteger)index
{
    if (_course && _course.courseClasses.count >= index)
    {
        _btnCls1.layer.borderColor = [UIColor blackColor].CGColor;
        _btnCls2.layer.borderColor = [UIColor blackColor].CGColor;
        _btnCls3.layer.borderColor = [UIColor blackColor].CGColor;
        
        _selectedClsIndex = index;
        _course.selectedClsIndex = index;
        
        _lblClassPrice.text = [NSString stringWithFormat:@"价格 : ￥%@", [_course.courseClasses[index - 1] classPrice]];
        NSString *classOpenDate = [NSDate monthStringForDateString:[_course.courseClasses[index - 1] classOpenDate]];
        
        switch (index) {
            case 1:
                _btnCls1.layer.borderColor = [WTFColors defaultIconColor].CGColor;
                _lblClass1.text = classOpenDate;
                break;
            case 2:
                _btnCls2.layer.borderColor = [WTFColors defaultIconColor].CGColor;
                _lblClass2.text = classOpenDate;
                break;
            case 3:
                _btnCls3.layer.borderColor = [WTFColors defaultIconColor].CGColor;
                _lblClass3.text = classOpenDate;
                break;
            default:
                break;
        }
    }
}

@end
